package dataAccessLayer.tests;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.ResourceBundle;


import junit.framework.Test;

import org.eclipse.hyades.test.common.junit.DefaultTestArbiter;
import org.eclipse.hyades.test.common.junit.HyadesTestCase;
import org.eclipse.hyades.test.common.junit.HyadesTestSuite;

import dataAccessLayer.dataBase.DBManager;
import dataAccessLayer.factoryMySQL.daos.DAOrecipesMySQL;
import dataAccessLayer.visualObject.VOrecipe;

/**
 * Generated code for the test suite <b>TestDAOrecipesMySQL</b> located at
 * <i>/Pastry/src/dataAccessLayer/tests/TestDAOrecipesMySQL.testsuite</i>.
 *
 * esta es la prueba a los metodos de la clase DAOrecipesMySQL
 */
public class TestDAOrecipesMySQL extends HyadesTestCase {
	/**
	 * Constructor for TestDAOrecipesMySQL.
	 * @param name
	 */
	
	private DBManager manager=new DBManager(ResourceBundle.getBundle("dataAccessLayer.dataAccess").getString("localConnectionType"));
	private DAOrecipesMySQL DAOrecipes=new DAOrecipesMySQL(manager);
	
	public TestDAOrecipesMySQL(String name) {
		super(name);
	}

	/**
	 * Returns the JUnit test suite that implements the <b>TestDAOrecipesMySQL</b> definition.
	 */
	public static Test suite() {
		HyadesTestSuite testDAOrecipesMySQL = new HyadesTestSuite(
				"TestDAOrecipesMySQL");
		testDAOrecipesMySQL.setArbiter(DefaultTestArbiter.INSTANCE).setId(
				"A1DD96663E4F06E0E162B43765653333");
		testDAOrecipesMySQL.addTest(new TestDAOrecipesMySQL("testInsertRecipe")
				.setId("A1DD96673054E1D0E162B43765653333").setTestInvocationId(
						"A1DD966919DF4CE0E162B43765653333"));
		testDAOrecipesMySQL.addTest(new TestDAOrecipesMySQL("testUpdateRecipe")
				.setId("A1DD966BCF392A00E162B43765653333").setTestInvocationId(
						"A1DD966C90363950E162B43765653333"));
		testDAOrecipesMySQL.addTest(new TestDAOrecipesMySQL("testRemoveRecipe")
				.setId("A1DD966D770C9D60E162B43765653333").setTestInvocationId(
						"A1DD966DCD003F10E162B43765653333"));
		testDAOrecipesMySQL.addTest(new TestDAOrecipesMySQL("testListRecipes")
				.setId("A1DD966DF6685FE0E162B43765653333").setTestInvocationId(
						"A1DD966F3F3F9250E162B43765653333"));
		testDAOrecipesMySQL.addTest(new TestDAOrecipesMySQL(
				"testSearchRecipeByCode").setId(
				"A1DD966F8DE14570E162B43765653333").setTestInvocationId(
				"A1DD96706220E750E162B43765653333"));
		testDAOrecipesMySQL.addTest(new TestDAOrecipesMySQL(
				"testSearchRecipeByName").setId(
				"A1DD966F95381600E162B43765653333").setTestInvocationId(
				"A1DD967063D2DD10E162B43765653333"));
		return testDAOrecipesMySQL;
	}

	/**
	 * @see junit.framework.TestCase#setUp()
	 */
	protected void setUp() throws Exception {
	}

	/**
	 * @see junit.framework.TestCase#tearDown()
	 */
	protected void tearDown() throws Exception {
	}

	/**
	 * testInsertRecipe
	 * @throws Exception
	 */
	public void testInsertRecipe() throws Exception 
	{
		VOrecipe vOrecipe= new VOrecipe();
		vOrecipe.setName("pastel del fresa");
		vOrecipe.setImage("D:");
		vOrecipe.setDescription("esta es la receta para hacer un pastel de fresa");
		this.DAOrecipes.insertRecipe(vOrecipe);
		
		PreparedStatement preparedStatement = null;
		ResultSet resultSet = null;

		Connection connection = manager.getConnection();
		String name="";
		
		try
		{
			preparedStatement = connection.prepareStatement("SELECT * FROM recipes ORDER BY code DESC LIMIT 1");
			resultSet = preparedStatement.executeQuery();
			
			if(resultSet.next())
			{
				name=resultSet.getString(2);
			}
			
		}
		catch (SQLException e)
		{
			e.printStackTrace();
		}
		finally
		{
			try
			{
				
				preparedStatement.close();
				resultSet.close();
			}
			catch (SQLException e)
			{
				e.printStackTrace();
			}
		}
		
		assertEquals(vOrecipe.getName(), name);
	}

	/**
	 * testUpdateRecipe
	 * @throws Exception
	 */
	public void testUpdateRecipe() throws Exception 
	{
		VOrecipe vOrecipe= new VOrecipe();
		vOrecipe.setCode(3);
		vOrecipe.setName("pastel del limon");
		vOrecipe.setImage("C:");
		vOrecipe.setDescription("esta es la receta para hacer un pastel de limon");
		
		this.DAOrecipes.updateRecipe(vOrecipe);
		
		PreparedStatement preparedStatement = null;
		ResultSet resultSet = null;

		Connection connection = manager.getConnection();
		String name="";
		
		try
		{
			preparedStatement = connection.prepareStatement("SELECT * FROM recipes where code=?");
			preparedStatement.setInt(1,vOrecipe.getCode());
			resultSet = preparedStatement.executeQuery();
			
			if(resultSet.next())
			{
				name=resultSet.getString(2);
			}
			
		}
		catch (SQLException e)
		{
			e.printStackTrace();
		}
		finally
		{
			try
			{
				
				preparedStatement.close();
				resultSet.close();
			}
			catch (SQLException e)
			{
				e.printStackTrace();
			}
		}
		
		assertEquals(vOrecipe.getName(), name);
	}

	/**
	 * testRemoveRecipe
	 * @throws Exception
	 */
	public void testRemoveRecipe() throws Exception 
	{	
		VOrecipe vOrecipe= new VOrecipe();
		vOrecipe.setCode(2);
		
		this.DAOrecipes.removeRecipe(vOrecipe);
		
		PreparedStatement preparedStatement = null;
		ResultSet resultSet = null;

		Connection connection = manager.getConnection();
		VOrecipe searchVoRecipe = new VOrecipe();
		
		try
		{
			preparedStatement = connection.prepareStatement("SELECT * FROM recipes where code=?");
			preparedStatement.setInt(1,vOrecipe.getCode());
			resultSet = preparedStatement.executeQuery();
			
			if(resultSet.next())
			{
				searchVoRecipe.setCode(resultSet.getInt(1));
				searchVoRecipe.setName(resultSet.getString(2));
			}
			else
			{
				searchVoRecipe=null;
			}
			
		}
		catch (SQLException e)
		{
			e.printStackTrace();
		}
		finally
		{
			try
			{
				
				preparedStatement.close();
				resultSet.close();
			}
			catch (SQLException e)
			{
				e.printStackTrace();
			}
		}
		assertEquals(searchVoRecipe,null );
	}

	/**
	 * testListRecipes
	 * @throws Exception
	 */
	public void testListRecipes() throws Exception
	{
		ArrayList<VOrecipe> search=this.DAOrecipes.listRecipes();
		
		PreparedStatement preparedStatement = null;
		ResultSet resultSet = null;

		Connection connection = manager.getConnection();
		
		int count=0;
		
		try
		{
			preparedStatement = connection.prepareStatement("SELECT * FROM recipes");
			resultSet = preparedStatement.executeQuery();
			
			while(resultSet.next())
			{
				count++;
			}
			
		}
		catch (SQLException e)
		{
			e.printStackTrace();
		}
		finally
		{
			try
			{
				
				preparedStatement.close();
				resultSet.close();
			}
			catch (SQLException e)
			{
				e.printStackTrace();
			}
		}
		
		assertEquals(search.size(),count );
	}

	/**
	 * testSearchRecipeByCode
	 * @throws Exception
	 */
	public void testSearchRecipeByCode() throws Exception 
	{
		VOrecipe vOrecipe= new VOrecipe();
		vOrecipe.setCode(1);
		
		VOrecipe search = this.DAOrecipes.searchRecipeByCode(vOrecipe);
		
		
		assertEquals(vOrecipe.getCode(),search.getCode());
	}

	/**
	 * testSearchRecipeByName
	 * @throws Exception
	 */
	public void testSearchRecipeByName() throws Exception 
	{
		VOrecipe vOrecipe= new VOrecipe();
		vOrecipe.setName("pastel de limon");
		
		
		VOrecipe search = this.DAOrecipes.searchRecipeByName(vOrecipe);
		
		
		assertEquals(vOrecipe.getCode(),search.getCode());
		
	}

}
